//
//  DDDiscoverBaseViewController.m
//  DingDongZP
//
//  Created by Jyh on 16/4/11.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDDiscoverBaseViewController.h"
#import "MJRefresh.h"

#define kTableViewHeight    kScreenHeight -44 - 49 -64


@interface DDDiscoverBaseViewController () <UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate>

/**
 *  无数据View
 */
@property (strong, nonatomic) UIView *noDataView;
@property (strong, nonatomic) UIImageView *noDataImageView;
@property (strong, nonatomic) UILabel *noDataLabel;

@end

@implementation DDDiscoverBaseViewController

#pragma mark
#pragma mark - View LifeCycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupData];
    [self setupSubViews];
    
    [self setNodataView];
}

#pragma mark
#pragma mark - Private Methods

/**
 *  设置无数据View
 */
- (void)setNodataView
{
    if (!self.noDataView) {
        self.noDataView = [[UIView alloc] init];
        self.noDataView.backgroundColor = COLOR_BACKGROUND;
        self.noDataView.frame = CGRectMake(0, 0, kScreenWidth, kTableViewHeight+44);
        self.noDataImageView = [[UIImageView alloc] init];
        self.noDataImageView.frame = CGRectMake(1, 1, 1, 1);
        self.noDataImageView.center = self.view.center;
        
        self.noDataLabel = [[UILabel alloc] init];
        self.noDataLabel.textColor = COLOR_FONT_DARK_GRAY;
        self.noDataLabel.numberOfLines = 0;
        self.noDataLabel.textAlignment = NSTextAlignmentCenter;
        self.noDataLabel.font = [UIFont fontWithName:@"Helvetica" size:16.];
        
        [self.noDataView addSubview:self.noDataImageView];
        [self.noDataView addSubview:self.noDataLabel];
    }
    
    
    [self.view addSubview:self.noDataView];
    self.noDataView.hidden = YES;
}

- (void)setupData
{
    self.dataSourceNoLogin = [NSMutableArray array];
}

- (void)setupSubViews
{
    self.view.backgroundColor = COLOR_BACKGROUND_GRAY;
    self.startIndexPageRecommend = 0;
    self.startIndexPageHottest = 0;
    self.startIndexPageLatest = 0;
    
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.tableViewRecommend];
    [self.scrollView addSubview:self.tableViewHotest];
    [self.scrollView addSubview:self.tableViewLatest];
    [self.view addSubview:self.tableViewNoLogin];
    
    self.tableViewRecommend.frame = CGRectMake(0, 0, kScreenWidth,kTableViewHeight);
    self.tableViewHotest.frame = CGRectMake(kScreenWidth, 0, kScreenWidth,kTableViewHeight);
    self.tableViewLatest.frame = CGRectMake(kScreenWidth * 2, 0, kScreenWidth,kTableViewHeight);
    
    [self addRefreshHeader];
    [self addRefreshFooter];
}

- (void)showNoDataViewWithImage:(UIImage *)image text:(NSString *)text
{
    if (!self.noDataView.hidden) {
        return;
    }
    
    self.noDataView.hidden = NO;
    
    if (image) {
        self.noDataImageView.image = image;
        
        float imageWidth = 0;
        float imageHeight = 0;
        if (image.size.width >400) {
            imageWidth = 400/2;
            imageHeight = image.size.height/image.size.width*200;
        } else {
            imageWidth = image.size.width;
            imageHeight = image.size.height;
        }
        
        [self.noDataImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view.mas_centerX);
            make.centerY.equalTo(self.view.mas_centerY).offset(-200);
            make.width.equalTo([NSNumber numberWithFloat:imageWidth]);
            make.height.equalTo([NSNumber numberWithFloat:imageHeight]);
        }];
        
        if (text && [text length] > 0) {
            self.noDataLabel.text = text;
            
            [self.noDataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self.view.mas_centerX);
                make.centerY.equalTo(self.noDataImageView.mas_bottom).offset(0);
                make.left.equalTo(self.view.mas_left).offset(20);
                make.right.equalTo(self.view.mas_right).offset(-20);
            }];
        }
    } else {
        if (text && [text length] > 0) {
            self.noDataLabel.text = text;
            
            [self.noDataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self.view.mas_centerX);
                make.centerY.equalTo(self.view.mas_centerY);
                make.left.equalTo(self.view.mas_left).offset(20);
                make.right.equalTo(self.view.mas_right).offset(-20);
            }];
        }
    }
}

- (void)hideNoDataView
{
    self.noDataView.hidden = YES;
}

#pragma mark
#pragma mark - Publich Method

- (void)addRefreshHeader
{
    WEAKSELF
    //兼容以前写法
    MJRefreshNormalHeader *header1 = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadNewData];
        [weakSelf loadNewData:self.tableViewRecommend];
    }];
    
    
    MJRefreshNormalHeader *header2 = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadNewData];
        [weakSelf loadNewData:self.tableViewHotest];
    }];
    
    
    MJRefreshNormalHeader *header3 = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadNewData];
        [weakSelf loadNewData:self.tableViewLatest];
    }];
    
    header1.lastUpdatedTimeLabel.hidden = true;
    header2.lastUpdatedTimeLabel.hidden = true;
    header3.lastUpdatedTimeLabel.hidden = true;
    
    self.tableViewRecommend.mj_header = header1;
    self.tableViewHotest.mj_header = header2;
    self.tableViewLatest.mj_header = header3;
}

- (void)addRefreshFooter
{
    //兼容以前写法
    WEAKSELF
    MJRefreshBackStateFooter *footer1 = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [weakSelf loadMoreData];
        [weakSelf loadMoreData:self.tableViewRecommend];
    }];
    
    MJRefreshBackStateFooter *footer2 = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [weakSelf loadMoreData];
        [weakSelf loadMoreData:self.tableViewHotest];
    }];
    
    MJRefreshBackStateFooter *footer3 = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [weakSelf loadMoreData];
        [weakSelf loadMoreData:self.tableViewLatest];
    }];
    
    self.tableViewRecommend.mj_footer = footer1;
    self.tableViewHotest.mj_footer = footer2;
    self.tableViewLatest.mj_footer = footer3;
}

- (void)loadNewData{};
- (void)loadMoreData{};
- (void)loadMoreData: (UITableView *)tableview{};
- (void)loadNewData: (UITableView *)tableview{};




- (void)setFooterHidden:(BOOL)hidden
{
    NSInteger page = (self.scrollView.contentOffset.x + self.scrollView.frame.size.width/2)/ self.scrollView.frame.size.width;
    
    if (page == 0) {
        // 推荐
        self.tableViewRecommend.mj_footer.hidden = hidden;
    } else if (page == 1) {
        // 最热
        self.tableViewHotest.mj_footer.hidden = hidden;
    } else if (page == 2) {
        // 最新
        self.tableViewLatest.mj_footer.hidden = hidden;
    }
}

//-(void) endRefreshFooterNoMoreData {
//    
//    [self.tableViewRecommend.mj_footer endRefreshingWithNoMoreData];
//    [self.tableViewLatest.mj_footer endRefreshingWithNoMoreData];
//    [self.tableViewHotest.mj_footer endRefreshingWithNoMoreData];
//}

- (void)endRefreshHeader
{
    [self.tableViewRecommend.mj_header endRefreshing];
    [self.tableViewLatest.mj_header endRefreshing];
    [self.tableViewHotest.mj_header endRefreshing];
}

- (void)endRefreshFooter
{
    [self.tableViewRecommend.mj_footer endRefreshing];
    [self.tableViewLatest.mj_footer endRefreshing];
    [self.tableViewHotest.mj_footer endRefreshing];
}

#pragma mark
#pragma mark - Event Response


#pragma mark
#pragma mark - CustomDelegate

#pragma mark -
#pragma mark  UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}

#pragma mark
#pragma mark - Getters and Setters

- (UIScrollView *)scrollView
{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.showsHorizontalScrollIndicator = NO;
        [_scrollView setContentSize:CGSizeMake(kScreenWidth * 3, 0 )];
        [_scrollView setFrame:CGRectMake(0, 44, kScreenWidth, kTableViewHeight)];
        [_scrollView setBackgroundColor:COLOR_BACKGROUND];
    }
    
    return  _scrollView;
}

- (UITableView *)tableViewRecommend
{
    if (_tableViewRecommend == nil) {
        _tableViewRecommend = [[UITableView alloc] init];
        _tableViewRecommend.tag = 1;
        _tableViewRecommend.delegate = self;
        _tableViewRecommend.dataSource = self;
        _tableViewRecommend.backgroundColor = COLOR_BACKGROUND_GRAY;

        _tableViewRecommend.rowHeight = HEIGHT_DISCOVER_CELL;
        _tableViewRecommend.tableFooterView = [[UIView alloc] init];
         _tableViewRecommend.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableViewRecommend.contentInset = UIEdgeInsetsMake(0, 0, 8, 0);
        
//        UINib *cellNib = [UINib nibWithNibName:@"DDDiscoverJobCell" bundle:nil];
//        [_tableViewRecommend registerNib:cellNib forCellReuseIdentifier:DDDiscoverJobCellIdentifier];
    }
    
    return _tableViewRecommend;
}

- (UITableView *)tableViewLatest
{
    if (_tableViewLatest == nil) {
        _tableViewLatest = [[UITableView alloc] init];
        _tableViewLatest.tag = 1;
        _tableViewLatest.delegate = self;
        _tableViewLatest.dataSource = self;
        _tableViewLatest.backgroundColor = COLOR_BACKGROUND_GRAY;
        _tableViewLatest.rowHeight = HEIGHT_DISCOVER_CELL;
        _tableViewLatest.tableFooterView = [[UIView alloc] init];
        _tableViewLatest.contentInset = UIEdgeInsetsMake(0, 0, 8, 0);
//        UINib *cellNib = [UINib nibWithNibName:@"DDDiscoverJobCell" bundle:nil];
//        [_tableViewLatest registerNib:cellNib forCellReuseIdentifier:DDDiscoverJobCellIdentifier];
        
        _tableViewLatest.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    
    return _tableViewLatest;
}

- (UITableView *)tableViewHotest {
    
    if (_tableViewHotest == nil) {
        _tableViewHotest = [[UITableView alloc] init];
        _tableViewHotest.tag = 2;
        _tableViewHotest.delegate = self;
        _tableViewHotest.dataSource = self;
        _tableViewHotest.backgroundColor = COLOR_BACKGROUND_GRAY;
        _tableViewHotest.rowHeight = HEIGHT_DISCOVER_CELL;
        _tableViewHotest.tableFooterView = [[UIView alloc] init];
        _tableViewHotest.contentInset = UIEdgeInsetsMake(0, 0, 8, 0);
//        UINib *cellNib = [UINib nibWithNibName:@"DDDiscoverJobCell" bundle:nil];
//        [_tableViewHotest registerNib:cellNib forCellReuseIdentifier:DDDiscoverJobCellIdentifier];
        
        _tableViewHotest.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    
    return _tableViewHotest;
}
- (UITableView *)tableViewNoLogin {
    
    if (_tableViewNoLogin == nil) {
        _tableViewNoLogin = [[UITableView alloc] init];
        _tableViewNoLogin.tag = 2;
        _tableViewNoLogin.delegate = self;
        _tableViewNoLogin.dataSource = self;
        _tableViewNoLogin.backgroundColor = COLOR_BACKGROUND_GRAY;
        _tableViewNoLogin.rowHeight = HEIGHT_DISCOVER_CELL;
        _tableViewNoLogin.tableFooterView = [[UIView alloc] init];
       
//        UINib *cellNib = [UINib nibWithNibName:@"DDDiscoverJobCell" bundle:nil];
//        [_tableViewNoLogin registerNib:cellNib forCellReuseIdentifier:DDDiscoverJobCellIdentifier];
        
        _tableViewNoLogin.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    
    return _tableViewNoLogin;
}

- (NSMutableArray *)dataSourceRecommend
{
    if (!_dataSourceRecommend) {
        _dataSourceRecommend = [NSMutableArray array];
    }
    
    return _dataSourceRecommend;
}

- (NSMutableArray *)dataSourceHotest
{
    if (!_dataSourceHotest) {
        _dataSourceHotest = [NSMutableArray array];
    }
    
    return _dataSourceHotest;
}

- (NSMutableArray *)dataSourceLatest
{
    if (!_dataSourceLatest) {
        _dataSourceLatest = [NSMutableArray array];
    }
    
    return _dataSourceLatest;
}

@end
